/*
 * Created on Jun 24, 2005 TODO To change the template for this generated file
 * go to Window - Preferences - Java - Code Style - Code Templates
 */
package com.novix.utility.web;

import org.apache.log4j.Logger;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import java.util.List;
import java.util.TimerTask;

import javax.servlet.ServletContext;

import lotus.domino.NotesException;
import lotus.domino.NotesFactory;
import lotus.domino.Session;

import com.novix.portal.framework.dao.DAOFactory;
import com.novix.portal.framework.dao.IWebContactDAO;
import com.novix.portal.framework.dao.IWebOrganizationDAO;
import com.novix.portal.framework.util.IConstants;
import com.opensymphony.oscache.general.GeneralCacheAdministrator;

/**
 * @author Dev TODO To change the template for this generated type comment go to
 *         Window - Preferences - Java - Code Style - Code Templates
 */
public class CacheCleaner extends TimerTask {
    /**
     * Logger for this class
     */
    private static final Logger logger = Logger.getLogger(CacheCleaner.class);

    private GeneralCacheAdministrator admin;
    private String                    corbaServerName;
    private String                    adminName;
    private String                    adminPassword;
    private String                    serverName;
    private String                    iePath;
    private ServletContext            context;

    /**
     * @param adminPassword
     * @param adminName
     * @param corbaServerName
     * @param iePath
     * @param serverName
     */
    public CacheCleaner(ServletContext context, GeneralCacheAdministrator admin, String corbaServerName, String adminName, String adminPassword, String serverName, String iePath) {
        super();
        this.admin = admin;
        this.corbaServerName = corbaServerName;
        this.adminName = adminName;
        this.adminPassword = adminPassword;
        this.serverName = serverName;
        this.iePath = iePath;
        this.context = context;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Runnable#run()
     */
    public void run() {

        try {     
            GeneralCacheAdministrator admin = (GeneralCacheAdministrator) context.getAttribute(IConstants.OS_CACHE_ADMIN);
            if (admin == null) {
                admin = new GeneralCacheAdministrator();
                context.setAttribute(IConstants.OS_CACHE_ADMIN, admin);
            }
            Session session = NotesFactory.createSession(corbaServerName, adminName, adminPassword);
            DAOFactory factory = DAOFactory.getDAOFactory(DAOFactory.DOMINO);
            IWebOrganizationDAO dao = factory.getWebOrganizationDAO();
            List list = dao.selectAllWebOrganizations(session, serverName, iePath);
            IWebContactDAO webContactDao = factory.getWebContactDAO();
            List contactList = webContactDao.selectAllWebContacts(session, serverName, iePath);
            List contactListAllPicsOrNo = webContactDao.selectAllWebContactsPicsOrNone(session, serverName, iePath);
            admin.flushEntry(IConstants.WEB_ORGANIZATION_CACHE);            
            admin.putInCache(IConstants.WEB_ORGANIZATION_CACHE, list);
            admin.flushEntry(IConstants.WEB_CONTACTS_CACHE);
            admin.putInCache(IConstants.WEB_CONTACTS_CACHE, contactList);
            admin.flushEntry(IConstants.WEB_CONTACTS_PICS_OR_NO_CACHE);       
            admin.putInCache(IConstants.WEB_CONTACTS_PICS_OR_NO_CACHE, contactListAllPicsOrNo);            
            context.setAttribute(IConstants.OS_CACHE_ADMIN, admin);
            logger.error("sending myself an email to see if this runs....");
        } catch (NotesException e) {
            logger.error("error in run impl for cachecleaner thread",e);
        }
    }

}
;